package com.hzh.page.controller.admin;


import com.hzh.page.common.enums.RightEnum;
import com.hzh.page.common.exception.ResponseEnum;
import com.hzh.page.common.utils.R;
import com.hzh.page.pojo.AuthenticationRoleEntity;
import com.hzh.page.service.AuthenticationMenuService;
import com.hzh.page.service.AuthenticationRoleMenuService;
import com.hzh.page.service.AuthenticationRoleService;
import com.hzh.page.vo.AuthenticationRoleMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 接口
 *
 * @author hzh
 * @date 2022-11-02
 */
@Api(tags = "角色信息")
@RestController
@RequestMapping(value = "/page/admin/role")
public class AuthenticationRoleController {
    @Autowired
    private AuthenticationRoleService roleService;

    @Autowired
    private AuthenticationRoleMenuService authenticationRoleMenuService;

    @Autowired
    private AuthenticationMenuService menuService;


    @ApiOperation("角色列表")
    @GetMapping("/listRole")
    public R listRole(HttpServletRequest request) {

        List<AuthenticationRoleEntity> list = roleService.list(null);
        return R.ok().put("roleList", list);
    }

    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    public R addRole(@RequestBody AuthenticationRoleMenuVO roleMenuVO, HttpServletRequest request) {
        boolean hashAuthority = menuService.hashAuthority(request, RightEnum.RIGHT_MANAGE_ROLE_UPDATE.getRoute_key());
        if (!hashAuthority) {
            return R.error(ResponseEnum.NO_RIGHT.getMessage());
        }
        AuthenticationRoleEntity roleEntity = new AuthenticationRoleEntity();
        BeanUtils.copyProperties(roleMenuVO, roleEntity);
        roleService.save(roleEntity);
        return R.ok("添加成功");
    }

    @ApiOperation("删除角色")
    @GetMapping("/deleteRole/{id}")
    public R addRole(@PathVariable("id") Integer id, HttpServletRequest request) {
        boolean hashAuthority = menuService.hashAuthority(request, RightEnum.RIGHT_MANAGE_ROLE_DELETE.getRoute_key());
        if (!hashAuthority) {
            return R.error(ResponseEnum.NO_RIGHT.getMessage());
        } else {
            AuthenticationRoleEntity roleInfo = roleService.getById(id);
            if (roleInfo == null) {
                return R.error("删除失败，不存在该角色");
            }
            roleService.removeById(id);
            return R.ok("删除成功");
        }
    }

    @ApiOperation("修改角色权限")
    @PostMapping("/updateRoleMenu")
    public R updateRoleMenu(@RequestBody AuthenticationRoleMenuVO authenticationRoleMenuVO, HttpServletRequest request) {
        boolean hashAuthority = menuService.hashAuthority(request, RightEnum.RIGHT_MANAGE_ROLE_UPDATE.getRoute_key());
        if (!hashAuthority) {
            return R.error(ResponseEnum.NO_RIGHT.getMessage());
        }
        authenticationRoleMenuService.updateRoleMenus(authenticationRoleMenuVO);
        return R.ok("修改成功");
    }

    @ApiOperation("获取角色拥有的权限信息")
    @GetMapping("/getRoleMenu/{id}")
    public R getRoleMenu(@PathVariable("id") Integer id) {
        AuthenticationRoleMenuVO roleMenuVO = roleService.getRoleMenuByRoleId(id);
        return R.ok().put("roleMenuVO", roleMenuVO);
    }
}
